﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.Entity.CleanChina;
using System.Data.SqlClient;
using System.Data;

namespace Torch.WL.DataAccess.CleanChina
{
    public class TbCompanyAccess
    {
        public TbCompanyAccess()
        {

        }


        public TbCompanyEntity GetModel(int companyId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from Tb_Company ");
            strSql.Append(" where companyId=@companyId");
            SqlParameter[] parameters = {
					new SqlParameter("@companyId", SqlDbType.Int,4)
			};
            parameters[0].Value = companyId;

            TbCompanyEntity model = new TbCompanyEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public TbCompanyEntity DataRowToModel(DataRow row)
        {
            TbCompanyEntity model = new TbCompanyEntity();
            if (row != null)
            {
                if (row["companyId"] != null && row["companyId"].ToString() != "")
                {
                    model.companyId = int.Parse(row["companyId"].ToString());
                }
                if (row["companyLogo"] != null)
                {
                    model.companyLogo = row["companyLogo"].ToString();
                }
                if (row["ZhanWeiH"] != null)
                {
                    model.ZhanWeiH = row["ZhanWeiH"].ToString();
                }
                if (row["location"] != null)
                {
                    model.location = row["location"].ToString();
                }
                if (row["companyName"] != null)
                {
                    model.companyName = row["companyName"].ToString();
                }
                if (row["companyTele"] != null)
                {
                    model.companyTele = row["companyTele"].ToString();
                }
                if (row["companyMemo"] != null)
                {
                    model.companyMemo = row["companyMemo"].ToString();
                }
                if (row["product"] != null)
                {
                    model.product = row["product"].ToString();
                }
                if (row["firstPingYinChar"] != null)
                {
                    model.firstPingYinChar = row["firstPingYinChar"].ToString();
                }

                if (row.Table.Columns.Contains("productTypeName") && row["productTypeName"] != null)
                {
                    model.productTypeName = row["productTypeName"].ToString();
                }

                

                if (row["createTime"] != null && row["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(row["createTime"].ToString());
                }
            }
            return model;
        }



        public List<TbCompanyEntity> QueryCompany(string querykey , int queryby)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@querykey", querykey),
                    new SqlParameter("@sortby", queryby)
			};

            DataSet ds = DbHelperSQL.RunProcedure("twsp_Company_Query", parameters, "ds");

            return DataTableToList(ds.Tables[0]);
        }

        public List<TbCompanyEntity> GetRewardCompany( int rewardId)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@rewardId", rewardId)
			};

            DataSet ds = DbHelperSQL.RunProcedure("twsp_Company_QueryByRewardId", parameters, "ds");

            return DataTableToList(ds.Tables[0]);
        }


        private List<TbCompanyEntity> DataTableToList(DataTable dt)
        {
            List<TbCompanyEntity> modelList = new List<TbCompanyEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TbCompanyEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }















    }
}
